package com.bjgzy.courselive.mvp.presenter;

import android.app.Application;

import com.bjgzy.courselive.mvp.contract.CoursePlayContract;
import com.bjgzy.courselive.mvp.model.entity.CoursePlayData;
import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.entity.CommonData;
import com.eduhzy.ttw.commonsdk.entity.CourseCommentData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;


/**
 * @author xubo
 * @date 2018-8-2 9:06:52
 */
@ActivityScope
public class CoursePlayPresenter extends BasePresenter<CoursePlayContract.Model, CoursePlayContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    private int page = 1;
    private int pageSize = 10;
    @Inject
    BaseQuickAdapter<CourseCommentData, AutoBaseViewHolder> mAdapter;
    @Inject
    public CoursePlayPresenter(CoursePlayContract.Model model, CoursePlayContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }


    public void getResourceDetail(boolean pullToRefresh, int internalId, int parentId) {
        if (pullToRefresh) {
            page = 1;
        } else {
            page++;
        }
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("internalId", internalId);
        map.put("parentId", parentId);
        map.put("userId", SPUtils.getInstance().getString(Constants.USERID));
        map.put("needCommentData", 0);
        map.put("pageNo", page);
        map.put("pageSize", pageSize);
        mModel.getResourceDetail(map)
                .retryWhen(new RetryWithDelay(2, 3))
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(disposable -> {
                    if (pullToRefresh)
                        mRootView.showLoading();//显示进度条
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    if (pullToRefresh)
                        mRootView.hideLoading();//隐藏进度条
                }).compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<CoursePlayData>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<CoursePlayData> commonData) {
                        if (commonData.isResult()) {
                            if (pullToRefresh) {
                                mRootView.update(commonData.getData());
//                                mAdapter.setNewData(commonData.getData().getCommetData().getData());
                            } else {
//                                mAdapter.addData(commonData.getData().getCommetData().getData());
                            }
//                            if (commonData.getData().getCommetData().getData().size() < pageSize) {
//                                mAdapter.loadMoreEnd(true);
//                            } else {
//                                mAdapter.loadMoreComplete();
//                            }
                        } else {
                            mRootView.showMessage(commonData.getMessage());
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        mRootView.loadFail();
                    }
                });
    }
}
